var SqlHelper = require('./sql.js')

class Dal {
    constructor(key, data, table) {
        // this.userData = userData
        // this.sqlHelper = new SqlHelper();
        this.data = data;
        this.primeryKey = key;
        this.sqlHelper = new SqlHelper(table);;
    }

    add() {
        return new Promise((resolve, reject) => {
            this.sqlHelper.insert(this.data, function (results) {
                resolve(results)
            })
        });
    }

    delete() {
        return new Promise((resolve, reject) => {
            this.sqlHelper.delete(this.primeryKey, function (results) {
                resolve(results)
            })
        })
    }

    update() {
        return new Promise((resolve, reject) => {
            this.sqlHelper.update(this.primeryKey, this.data, function (results) {
                resolve(results)
            })
        })
    }

    select() {
        return new Promise((resolve, reject) => {
            this.sqlHelper.select(this.primeryKey, function (results) {
                resolve(results)
            })
        });
    }

    selectAll(offset, limit, orderby, sort) {
        return new Promise((resolve, reject) => {
            this.sqlHelper.select(null, function (results) {
                resolve(results)
            }, offset, limit, orderby, sort)
        });
    }

    comments() {
        return new Promise((resolve, reject) => {
            this.sqlHelper.comments(function (results) {
                resolve(results)
            })
        });
    }

    count() {
        return new Promise((resolve, reject) => {
            this.sqlHelper.count(function (results) {
                resolve(results[0].num)
            })
        });
    }
}
module.exports = Dal;